package com.neusoft.student.controller;

import com.neusoft.student.model.Student;
import com.neusoft.student.service.StudentService;
import com.neusoft.student.service.impl.StudentServiceImpl;
import com.neusoft.student.utils.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author: duan
 * @Date: Create in 9:56 2020/10/21
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
        //接收表单提交的参数
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        String rem = req.getParameter("rem");
        RequestUtil.printRequestParams(req);

        if ("0".equals(type)) {
            //系统管理员
        } else if ("1".equals(type)) {
            //宿舍管理员
        } else if ("2".equals(type)) {
            //学生
            StudentService studentService = new StudentServiceImpl();
            Student bean = studentService.login(account, password);
            if (bean == null) {
//                req.setAttribute("msg", "账号或密码错误");
//                req.getRequestDispatcher("login.jsp").forward(req, resp);
                resp.sendRedirect(getServletContext().getContextPath() + "/login.jsp?error=501");
            } else {
//                req.getRequestDispatcher("student_list.jsp").forward(req, resp);
                req.getSession().setAttribute("username", account);
                resp.sendRedirect(getServletContext().getContextPath() + "/main.jsp");
            }
        }
    }
}
